package game.assist.diablo3.ui;

import game.assist.diablo3.R;
import game.assist.diablo3.data.vo.GoodsCategory;

import java.util.ArrayList;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;

public class VerticalTabPageIndicator extends ScrollView {

	Runnable mTabViewSelectorRunnable;

	private OnClickListener mTabClickListener = new OnClickListener() {
		@Override
		public void onClick(View view) {
			TabView tabView = (TabView) view;
			// tabView clicked
			// mViewPager.setCurrentItem(tabView.getIndex());
		}
	};

	public VerticalTabPageIndicator(Context context) {
		this(context, null);
	}

	public VerticalTabPageIndicator(Context context, AttributeSet attrs) {
		super(context, attrs);
		mInflater = LayoutInflater.from(context);

		mTabViewsLinearLayout = new LinearLayout(getContext());
		mTabViewsLinearLayout.setBackgroundColor(android.R.color.black);
		addView(mTabViewsLinearLayout, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
	}

	private LinearLayout mTabViewsLinearLayout;
	private LayoutInflater mInflater;

	int mMaxTabWidth;
	private int mSelectedTabIndex;

	@Override
	public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
		final int widthMode = MeasureSpec.getMode(widthMeasureSpec);
		final boolean lockedExpanded = widthMode == MeasureSpec.EXACTLY;
		setFillViewport(lockedExpanded);

		final int childCount = mTabViewsLinearLayout.getChildCount();
		if (childCount > 1 && (widthMode == MeasureSpec.EXACTLY || widthMode == MeasureSpec.AT_MOST)) {
			if (childCount > 2) {
				mMaxTabWidth = (int) (MeasureSpec.getSize(widthMeasureSpec) * 0.4f);
			} else {
				mMaxTabWidth = MeasureSpec.getSize(widthMeasureSpec) / 2;
			}
		} else {
			mMaxTabWidth = -1;
		}

		final int oldWidth = getMeasuredWidth();
		super.onMeasure(widthMeasureSpec, heightMeasureSpec);
		final int newWidth = getMeasuredWidth();

		if (lockedExpanded && oldWidth != newWidth) {
			// Recenter the tab display if we're at a new (scrollable) size.
			// setCurrentItem(mSelectedTabIndex);
		}
	}

	private void animateToTab(final int position) {
		final View tabView = mTabViewsLinearLayout.getChildAt(position);
		if (mTabViewSelectorRunnable != null) {
			removeCallbacks(mTabViewSelectorRunnable);
		}
		mTabViewSelectorRunnable = new Runnable() {
			public void run() {
				final int scrollPos = tabView.getLeft() - (getWidth() - tabView.getWidth()) / 2;
				smoothScrollTo(scrollPos, 0);
				mTabViewSelectorRunnable = null;
			}
		};
		post(mTabViewSelectorRunnable);
	}

//	@Override
//	public void onAttachedToWindow() {
//		super.onAttachedToWindow();
//		if (mTabViewSelectorRunnable != null) {
//			// Re-post the selector we saved
//			post(mTabViewSelectorRunnable);
//		}
//	}

//	@Override
//	public void onDetachedFromWindow() {
//		super.onDetachedFromWindow();
//		if (mTabViewSelectorRunnable != null) {
//			removeCallbacks(mTabViewSelectorRunnable);
//		}
//	}

	private void addTab(String text, int indexId) {
		// Workaround for not being able to pass a defStyle on pre-3.0
//		final VerticalTabView tabView = (VerticalTabView) mInflater.inflate(R.layout.vpi__tab_vertical, null);
		final VerticalTabView tabView = (VerticalTabView) mInflater.inflate(R.layout.tmp_vpi__tab_vertical, null);
		tabView.init(this, text, indexId);
		tabView.setFocusable(true);
		tabView.setOnClickListener(mTabClickListener);

		mTabViewsLinearLayout.addView(tabView, new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 1));
		int c = mTabViewsLinearLayout.getChildCount();
		int t = this.getChildCount();
	}

	public void notifyDataSetChanged(ArrayList<GoodsCategory> categorys) {
		mTabViewsLinearLayout.removeAllViews();
		// TitleProvider adapter = (TitleProvider) mViewPager.getAdapter();
		// final int count = ((PagerAdapter) adapter).getCount();
		final int count = categorys.size();
		for (int i = 0; i < count; i++) {
			GoodsCategory category = categorys.get(i);
			addTab(category.getCategoryName(), category.getCategoryId());
		}
		if (mSelectedTabIndex > count) {
			mSelectedTabIndex = count - 1;
		}
		// setCurrentItem(mSelectedTabIndex);
		requestLayout();
	}

	public static class VerticalTabView extends LinearLayout {
		protected VerticalTabPageIndicator mParent;
		protected int mIndex;

		public VerticalTabView(Context context, AttributeSet attrs) {
			super(context, attrs);
		}

		public void init(VerticalTabPageIndicator parent, String text, int index) {
			mParent = parent;
			mIndex = index;

			// display title
			TextView textView = (TextView) findViewById(android.R.id.text1);
			// TabView textSize
			textView.setTextSize(17f);
			textView.setEms(2);
			textView.setBackgroundColor(android.R.color.black);
			// TabPageIndicator's Indicator width
			textView.setPadding(20, 0, 20, 0);
			textView.setText(text);
		}

		@Override
		public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
			super.onMeasure(widthMeasureSpec, heightMeasureSpec);
			// Re-measure if we went beyond our maximum size.
			if (mParent.mMaxTabWidth > 0 && getMeasuredWidth() > mParent.mMaxTabWidth) {
				super.onMeasure(MeasureSpec.makeMeasureSpec(mParent.mMaxTabWidth, MeasureSpec.EXACTLY), heightMeasureSpec);
			}
		}

		public int getIndex() {
			return mIndex;
		}
	}

}
